package com.tang.mybatis.session;

import com.tang.mybatis.binding.MapperRegistry;
import com.tang.mybatis.mapping.MappedStatement;
import com.tang.mybatis.session.defaults.DefaultSqlSession;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author Tang
 * @classname Configuration
 * @description [ 配置类 ]
 * @date 2022/4/6 16:07
 */
public class Configuration {

    /**
     * 配置项
     */
    public static Properties properties = new Properties();

    /**
     * sql 语句
     */
    protected Map<String, MappedStatement> statementMap = new HashMap<>();

    /**
     * mapper 代理注册器
     */
    protected final MapperRegistry mapperRegistry = new MapperRegistry();

    public void addMapper(Class clz) {
        mapperRegistry.addMapper(clz);
    }

    public <T> T getMapper(Class<T> tClass, SqlSession sqlSession) {
        return mapperRegistry.getMapper(tClass,sqlSession);
    }

    public void addMappedStatement(String id,MappedStatement mappedStatement) {
        statementMap.put(id, mappedStatement);
    }

    public MappedStatement getMappedStatement(String id) {
        return statementMap.get(id);
    }

    public static String getProperty(String key) {
        return getProperty(key, "");
    }

    private static String getProperty(String key, String defaultValue) {
        return properties.getProperty(key, defaultValue);
    }

}